public class LinkedListQueue<E> implements Queue<E> {
    public class Node{
        public E e;
        public Node next;

        public Node(E e, Node next){
            this.e = e;
            this.next = next;
        }
        public Node(E e){
            this(e, null);
        }
        public Node(){
            this(null,null);
        }

        @Override
        public String toString(){
            return e.toString();
        }
    }
    private Node dummyHead;
    private Node head, tail;
    private int size;

    public LinkedListQueue(){
        head = null;
        tail = null;
        size = 0;
        dummyHead = new Node(null,null);
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size ==0;
    }

    public void enqueue(E e) {
        if(tail == null){
            Node prev = dummyHead;

            tail = new Node(e);
            head = tail;
            prev.next = head;
        }else {

            //创建一个新的节点Node，然后tail.next指向这个新的节点，
            // 接着把tail设置成Node.next，维护tail指针的位置
            tail.next = new Node(e);
            tail = tail.next;
        }
        size++;
    }

    public E dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        }
        //retNode节点也就是出队元素所在节点应该是head这个位置所指的节点
        //retNode指向头节点head的下一个节点
        Node retNode = head;
        //新的head直接跳过retNode指向head的下一个节点
        head = head.next;
        //新的head为原来head的下一个节点，原来head为retNode
        retNode.next = null;
        //如果head下一个节点为null，则tail也为null
        if(head == null){
            tail = null;
        }
        return retNode.e;
    }

    public E getFront() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Queue is empty.");
        }
        return head.e;
    }

    public E contains(E e){
        Node cur = head;
        CarInfo info = (CarInfo) cur.e;
        while (cur != null){
            if(info.license.equals(e)){
                return (E) info;
            }
            cur = cur.next;
        }
        return null;

    }

    public E containsAndremove(E e){
        Node prev = dummyHead;
//        Node cur = head;
        CarInfo info;
        while (prev.next != null){
            info = (CarInfo) prev.next.e;
            if(info.license.equals(e)){
                Node retNode = prev.next;
                prev.next = retNode.next;
                retNode.next = null;
                size --;
                return (E) info;
            }
            prev = prev.next;
        }
        return null;

    }

    public E getData(int e) {
        return null;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append("Queue: front ");
        Node cur = head;
//        Node cur = dummyHead.next;
        while (cur != null){
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL tail");
        return res.toString();

    }
    public static void main(String[] args) {
        LinkedListQueue<Integer> queue = new LinkedListQueue<Integer>();
        for(int i =0 ; i < 10 ; i++){
            queue.enqueue(i);
            System.out.println(queue);
            if(i % 3 ==2){
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}
